//
//  Emoticons.swift
//  表情键盘
//
//  Created by 雄 on 15/7/20.
//  Copyright © 2015年 雄. All rights reserved.
//

import UIKit

class EmoticonsPackage: NSObject {
    
    var id: String?
    var groupName: String?
    var emoticons: [Emoticon]?
    
    
    class func packages() -> [EmoticonsPackage]{
        var list = [EmoticonsPackage]()
        
        // 0. '最近' 表情包设置
        let near = EmoticonsPackage()
        near.id = ""
        near.groupName = "最近"
        //  添加空白表情
        near.appendBlankEmoticon()
        list.append(near)
        
        // 1. emoticons.plist 包含的表情包设置
       let path = NSBundle.mainBundle().pathForResource("emoticons.plist", ofType: nil, inDirectory: "Emoticons.bundle")!
       let dict = NSDictionary(contentsOfFile: path)!
       let array = dict["packages"] as! [[String: AnyObject]]
        
        for dic in array {
           let e = EmoticonsPackage()
            e.id = dic["id"] as? String
            
            e.loadEmoticon()
            // 2. 添加空白表情
            e.appendBlankEmoticon()
            
            list.append(e)
        }
        
        return list
    }
    
    // 加载表情数组
    private func loadEmoticon() {
 
        let path = EmoticonsPackage.bundlePath().stringByAppendingPathComponent(id!).stringByAppendingPathComponent("info.plist")
        
        let dict = NSDictionary(contentsOfFile: path)!
        // groupName
        groupName = dict["group_name_en"] as? String
        // emoticons
        let array = dict["emoticons"] as! [[String: AnyObject]]
        
        var emoticonsArray = [Emoticon]()
        
        var index = 0
        for dics in array {
            emoticonsArray.append(Emoticon(id: id, dict: dics))
            
            index++
            // 最后一个插入一个删除按钮
            if  index == 20 {
                emoticonsArray.append(Emoticon(isRemoveBtn: true))
                index = 0
            }
        }
        
        emoticons = emoticonsArray
    }
    
    // '最近' 表情分组,添加常点击的表情
    func addNearSelectedEmoticon(emoticon: Emoticon) {
//       
//        if !emoticons!.contains(emoticon) {
//            emoticons?.append(emoticon)
//    
//            var result = emoticons!
//        
//            result =  result.sort { (a1, a2) -> Bool in
//                a1.times > a2.times
//            }
//        
//            result.removeLast()
//            emoticons = result
//        } else {
//            var result = emoticons!
//            
//            result =  result.sort { (a1, a2) -> Bool in
//                a1.times > a2.times
//            }
//            
//            emoticons = result
//        }
        
        // 删除最后一个删除按钮
        emoticons?.removeLast()
        let contain = emoticons!.contains(emoticon)
        if !contain {
            emoticons?.append(emoticon)
        }
        
        var result = emoticons!
        
        result =  result.sort { (a1, a2) -> Bool in
            a1.times > a2.times
        }
        
        if !contain {
            // 删除最后一个空白表情
            result.removeLast()
        }
        
        result.append(Emoticon(isRemoveBtn: true))
        emoticons = result
        
    }
    
    // 添加空白表情
    private func appendBlankEmoticon() {
        
        if  emoticons == nil {
            emoticons = [Emoticon]()
        }
        
        let count = emoticons!.count % 21
        if count > 0 || emoticons!.count == 0{
            for _ in count..<20 {
                // 追加空白按钮
                emoticons?.append(Emoticon(isRemoveBtn: false))
            }
            // 追加删除按钮
            emoticons?.append(Emoticon(isRemoveBtn: true))
        }
    }
    
    // 表情文件的路径
    class func bundlePath() -> String{
        return NSBundle.mainBundle().bundlePath.stringByAppendingPathComponent("Emoticons.bundle")
    }
    
}

// 表情模型
class Emoticon: NSObject {
    
    var id: String?
    var chs: String?
    var png: String?
    
    // 图片实际路径
    var imagePath: String? {
        return png == nil ? nil : EmoticonsPackage.bundlePath().stringByAppendingPathComponent(id!).stringByAppendingPathComponent(png!)
    }
    
    // UNICODE 编码字符串
    var code: String? {
        didSet{
            // 1. 扫描器,扫面字符中制定格式的内容
            let scanner = NSScanner(string: code!)
            // 2. 扫描整数,要修改参数内存指针的内容
            var result: UInt32 = 0
            scanner.scanHexInt(&result)
            // 3. UNICODE 字符 -> 字符串
            emoji = "\(Character(UnicodeScalar(result)))"
        }
    }
    // code 转换后的 emoji 表情字符串
    var emoji: String?
    // 是否为删除按钮
    var removeBtn = false
    // 被点击的次数
    var times = 0
    
    init(isRemoveBtn: Bool) {
        removeBtn = isRemoveBtn
    }
    
    init(id: String?, dict: NSDictionary) {
        super.init()
        
        self.id = id
        setValuesForKeysWithDictionary(dict as! [String : AnyObject])
    }
    
    // 只要重写这个方法,有未实现的属性时,不会崩掉
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
    }
}


